package com.aaa.sync;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aaa.dto.GetDjhUpdateDateTimeDto;
import com.aaa.entity.TSetAncientSingletonAllocation;
import com.aaa.properties.NationalReportProperty;
import com.aaa.service.TSetAncientSingletonAllocationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : luoBo
 */
@Slf4j
@Component
public class AncientSingletonAllocationSync {

    @Autowired
    private TSetAncientSingletonAllocationService tSetAncientSingletonAllocationService;

    @Autowired
    private NationalReportProperty nationalReportProperty;


    @Transactional(rollbackFor = Exception.class)
    public void sync() {

        log.info(">>>>>>>>>>>>>>>>开始执行古法单件调拨码表定时任务<<<<<<<<<<<<<<<<<<<<");
        GetDjhUpdateDateTimeDto dto = new GetDjhUpdateDateTimeDto();
        String updateTime = tSetAncientSingletonAllocationService.getUpdateTimeMax();

        //dto.setUpdateTime(updateTime);
        try (HttpResponse response = HttpRequest.post(nationalReportProperty.getIpPort()+"/api/setAncientSingletonAllocationApi/getUpdateDateTime")
                .header("Content-Type", "application/json")
                .body(JSONUtil.toJsonStr(dto))
                .execute()) {
            if (response.getStatus() == 200) {
                log.info("古法单件调拨码表定时任务执行成功");
                String body = response.body();
                // 解析 JSON 数据
                JSONObject jsonObject = JSONUtil.parseObj(body);
                log.info("古法单件调拨码表定时任务json数据:{}",jsonObject);
                // 提取字段
                int code = jsonObject.getInt("code");
                String message = jsonObject.getStr("message");
                JSONArray result = jsonObject.getJSONArray("result");
                List<TSetAncientSingletonAllocation> remoteList  = result.toList(TSetAncientSingletonAllocation.class);
                if (CollectionUtil.isNotEmpty(remoteList)){
                    List<TSetAncientSingletonAllocation> inserList = new ArrayList<>();
                    List<TSetAncientSingletonAllocation> updateList = new ArrayList<>();
                    List<TSetAncientSingletonAllocation> localList = tSetAncientSingletonAllocationService.list();
                    if (CollectionUtil.isNotEmpty(localList)){
                        Map<String, TSetAncientSingletonAllocation> identityLocalMap = localList.stream().collect(Collectors.toMap(TSetAncientSingletonAllocation::getIdentity
                                , t -> t
                                , (t1, t2) -> t1));
                        // 远程数据中不存在本地数据
                        inserList = remoteList.stream().filter(s->!identityLocalMap.containsKey(s.getIdentity())).collect(Collectors.toList());
                        // 远程数据中存在本地数据
                        updateList = remoteList.stream().filter(s->identityLocalMap.containsKey(s.getIdentity())).collect(Collectors.toList());
                    }
                    if (CollectionUtil.isNotEmpty(inserList)){
                        tSetAncientSingletonAllocationService.saveBatch(remoteList);
                        log.info("放入简单的的数据");
                    }
                    if (CollectionUtil.isNotEmpty(updateList)){
                        tSetAncientSingletonAllocationService.updateBatchById(updateList);
                    }
                }
            }else {
                log.info("古法单件调拨码表定时任务执行失败");
            }
        }
    }
}
